package user

import (
	"bolg_admin/pkg/logger"
	"bolg_admin/pkg/model"
)

type Verify struct {
	ID       uint   `json:"id"`
	Password string `json:"password"`
}

type InfData struct {
	Name string `json:"name"`
}

// Create 创建用户，通过 User.ID 来判断是否创建成功
func (user *User) Create() (err error) {
	if err = model.DB.Create(&user).Error; err != nil {
		logger.LogError(err)
		return err
	}
	return nil
}

//GetByName 登录
func GetByName(name string, password string) (uint, bool) {
	var user Verify
	err := model.DB.Table("users").Select("id", "password").Where("name = ?", name).Scan(&user).Error

	if err != nil || password != user.Password {
		return user.ID, false
	}
	return user.ID, true
}

func GetUserInf(id interface{}) (inf InfData, err error) {
	err = model.DB.Model(&User{}).Where("id = ?", id).Find(&inf).Error
	if err != nil {
		logger.LogError(err)
		return inf, err
	}
	return inf, nil

}
